/*
 * 版权属于onps栈开发团队，遵循Apache License 2.0开源许可协议
 *
 */
#include "port/datatype.h"
#include "port/sys_config.h"
#include "onps_errors.h"
#include "port/os_datatype.h"
#include "port/os_adapter.h"
#include "one_shot_timer.h"
#include "onps_utils.h"
#include "protocols.h"
#include "onps_input.h"
#include "netif/netif.h"
#include "los_task.h"

#if NETTOOLS_TELNETSRV
#include "ch32v30x.h"
#include "ch32v30x_eth.h"

#if NETTOOLS_PING && NVTCMD_PING_EN
#include "los_arch_timer.h"
#endif //* #if NETTOOLS_PING && NVTCMD_PING_EN

#if SUPPORT_ETHERNET && NVTCMD_IFIP_EN
//#include "at24c02.h"
#endif //* #if SUPPORT_ETHERNET && NVTCMD_IFIP_EN

#if NETTOOLS_SNTP && NVTCMD_NTP_EN
#include "rtc.h"
#endif //* #if NETTOOLS_SNTP && NVTCMD_NTP_EN

#define SYMBOL_GLOBALS
#include "telnet/os_nvt.h"
#undef SYMBOL_GLOBALS

#define INVALID_TASK_ID 0xFFFFFFFF

#define THNVT_STK_SIZE 1180
static const UCHAR l_ubaTHNvtPrio[NVTNUM_MAX] = {THNVT0_PRIO, THNVT1_PRIO}; 
static struct {
  UINT unTaskId;
  void *pvParam;
} l_staTHNvt[NVTNUM_MAX];
extern void thread_nvt_handler(void *pvParam); 
void os_nvt_init(void)
{
  UCHAR i;
  for(i=0; i<NVTNUM_MAX; i++)
    l_staTHNvt[i].unTaskId = INVALID_TASK_ID;
}

void os_nvt_uninit(void)
{
}

BOOL os_nvt_start(void *pvParam)
{
  UCHAR i; 
  for(i=0; i<NVTNUM_MAX; i++)
  {
    if(INVALID_TASK_ID == l_staTHNvt[i].unTaskId)
    {
      UINT unRtnVal;
      TSK_INIT_PARAM_S stTInitParam = { 0 };

      l_staTHNvt[i].pvParam     = pvParam;
      stTInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)thread_nvt_handler;
      stTInitParam.usTaskPrio   = (USHORT)l_ubaTHNvtPrio[i];
      stTInitParam.uwArg        = (UINT)pvParam;
      stTInitParam.uwStackSize  = THNVT_STK_SIZE;
      stTInitParam.pcName       = "thread_nvt_handler";
      unRtnVal = LOS_TaskCreate(&l_staTHNvt[i].unTaskId, &stTInitParam);
      if (LOS_OK == unRtnVal)
        return TRUE;

      l_staTHNvt[i].unTaskId = INVALID_TASK_ID;

      printf("The creation of task thread_nvt_handler-%d failed, error code: %08X\r\n", i, unRtnVal);
      return FALSE;
    }
  }
  
  return FALSE; 
}

void os_nvt_stop(void *pvParam)
{
  UCHAR i; 
  for(i=0; i<NVTNUM_MAX; i++)
  {
    if(pvParam == l_staTHNvt[i].pvParam && INVALID_TASK_ID != l_staTHNvt[i].unTaskId)
    {
      LOS_TaskDelete(l_staTHNvt[i].unTaskId);
      l_staTHNvt[i].unTaskId = INVALID_TASK_ID;
    }
  }
}

#if SUPPORT_ETHERNET && NVTCMD_IFIP_EN
#if ETH_EXTRA_IP_EN
BOOL os_nvt_add_ip(const CHAR *pszIfName, in_addr_t unIp, in_addr_t unSubnetMask)
{
#warning "os_nvt_add_ip() not yet implemented"
  return TRUE;
}

BOOL os_nvt_del_ip(const CHAR *pszIfName, in_addr_t unIp)
{
#warning "os_nvt_del_ip() not yet implemented"
  return TRUE;
}
#endif //* #if ETH_EXTRA_IP_EN 
BOOL os_nvt_set_ip(const CHAR *pszIfName, in_addr_t unIp, in_addr_t unSubnetMask, in_addr_t unGateway)
{
#warning "os_nvt_set_ip() not yet implemented"
  return TRUE;
}

BOOL os_nvt_set_mac(const CHAR *pszIfName, const CHAR *pszMac)
{
#warning "os_nvt_set_mac() not yet implemented"
  return TRUE;
}

BOOL os_nvt_set_dns(const CHAR *pszIfName, in_addr_t unPrimaryDns, in_addr_t unSecondaryDns)
{
#warning "os_nvt_set_dns() not yet implemented"
  return TRUE;
}

BOOL os_nvt_set_dhcp(const CHAR *pszIfName)
{
#warning "os_nvt_set_dhcp() not yet implemented"
  return TRUE;
}

void os_nvt_system_reset(void)
{
  NVIC_SystemReset(); 
}
#endif //* #if SUPPORT_ETHERNET && NVTCMD_IFIP_EN

#if NVTCMD_ROUTE_EN
BOOL os_nvt_add_route_entry(const CHAR *pszIfName, in_addr_t unDestination, in_addr_t unGenmask, in_addr_t unGateway)
{
#warning "os_nvt_add_route_entry() not yet implemented"
  return TRUE;
}

BOOL os_nvt_del_route_entry(in_addr_t unDestination)
{
#warning "os_nvt_del_route_entry() not yet implemented"
  return TRUE;
}
#endif //* #if NVTCMD_ROUTE_EN

#if NETTOOLS_PING && NVTCMD_PING_EN
UINT os_get_elapsed_millisecs(void)
{
  return HalGetElapsedMSecs();
}
#endif //* #if NETTOOLS_PING && NVTCMD_PING_EN

#if NETTOOLS_SNTP && NVTCMD_NTP_EN
void os_nvt_set_system_time(time_t tNtpTime)
{
  RTCSetSystemUnixTimestamp(tNtpTime); 
}
#endif //* #if NETTOOLS_SNTP && NVTCMD_NTP_EN

#endif //* #if NETTOOLS_TELNETSRV
